#!/usr/bin/perl

use strict;
use warnings;

# Copyright 2009 David Lloyd
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

=head1 NAME

TranslateJavascriptToLSL - Perform basic transformation of Javascript to LSL

=head1 VERSION

Version 0..0.4

=head1 SYNOPSIS

    # Paste the Javascript into the DATA section
    #
    # Run the script
    # % perl parse_simple_array.pl
    #
    # By default this outputs to STDOUT. On UNIX, Linux or OS X you can:
    #
    # % perl parse_simple_array.pl > an_lsl_file.lsl
    
=cut

our $VERSION = "0.0.3";

my $varFlag = 0;
my $endFlag = 0;

my $simpleLineFlag  = 0;
my $complexLineFlag = 0;

my $var    = "";
my $spaces = " " x 4;

my $separator = "DSL-";

my $lines   = 0;
my $current = "";

my @lines = ();

my @complex_lines = ();

while (<DATA>) {
    chomp;

    my $data = strip_ws($_);

    next if $data eq "";

    $varFlag = $data =~ m/^var (\w+) = new Array\(/;

    # For the moment assume that each will be on an individual line.
    if ($varFlag) {
        $var = $1;
        $lines++;
        @complex_lines = ();
        next;
    }

    # The end of a simple entry.
    $endFlag = $data =~ m/(".*")\);$/;

    if ($endFlag) {
        $var = "";
        $current .= "$spaces$1\n];\n\n";
        push @lines, $current;
        $current = "";
        $lines   = 0;
        next;
    }

    # The end of a complex entry
    $endFlag = $data =~ m/\[(.*)\]\);/;

    if ($endFlag) {
        $current .= "$spaces\"$separator$lines\", $1];\n\n";
        $var = "";
        push @lines, $current;
        $current = "";
        $lines   = 0;
        next;
    }

    # A simple entry
    $simpleLineFlag = $data =~ m/^(".*"),$/;

    if ($simpleLineFlag) {
        if ( scalar @complex_lines < 1 ) {
            if ( $lines == 1 ) {
                $current .= "list $var = [\n";
            }

            $current .= "$spaces$1, \n";
            $lines++;
            next;
        }
        else {
            push @complex_lines, $1;
            next;
        }
    }

    # A complex line because it will span more than one
    $complexLineFlag = $data =~ m/\[(.*)\],$/;

    if ($complexLineFlag) {
        if ( $lines == 1 ) {
            $current .= "list $var = [\n";
        }

        $current .= "$spaces\"$separator$lines\", $1,\n";
        $lines++;
        next;
    }

    # The start of a complex entry that may span more than one line
    $complexLineFlag = $data =~ m/\[(.*),$/;

    if ($complexLineFlag) {
        if ( $lines == 1 ) {
            $current .= "list $var;\n";
        }

        push @complex_lines, $1;
        next;
    }

    # The end of a complex entry
    $endFlag = $data =~ m/^(".*")],$/;

    if ($endFlag) {
        push @complex_lines, $1;
        my $line_contents = join( ", ", @complex_lines );
        $current .= "$spaces\"$separator$lines\", $line_contents,";
        push @lines, $current;
        $current = "";
        $lines++;
        @complex_lines = ();
        next;
    }

    $endFlag = $data =~ m/^(".*")\]\);$/;

    if ($endFlag) {

        push @complex_lines, $1;
        my $line_contents = join( ", ", @complex_lines );
        $current .= "$spaces\"$separator$lines\", $line_contents];\n\n";
        push @lines, $current;
        $current       = "";
        $lines         = 0;
        @complex_lines = ();
        next;
    }

    warn "Didn't match anything for ($data) - or there's a missing next.\n";
}

foreach my $l (@lines) {
    print "$l\n";
}

sub strip_ws {
    my $data = shift;

    $data =~ s/^\s*//g;
    $data =~ s/\s*$//g;

    return $data;
}

=head1 AUTHOR

"David Lloyd", C<< <"<lloy0076 at adam.com.au>"> >>

=head1 BUGS

=over

=item *

The main loop only copes with one style of Javascript; woe if a different
style exists.

=item *

The main loop should I<at least> be refactored into a series of functions.

=item *

This should be made a module, not a script.

=item *

One should be able to specify the Javascript file to translate from the
command line, or give one or more file names.

=item *

I really should be using Getopt (at least) - I prefer Getopt::Long though.

=item *

It won't do a translation from all of Javascript to LSL; I'm only making it
good enough for arrays at the moment.

=back

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc TestJSON

=head1 SEE ALSO

=over

=item *

Linden Lab's wiki, wiki.secondlife.com

=item *

LSL Wiki, lslwiki.net

=back

=head1 COPYRIGHT & LICENSE

# Copyright 2009 David Lloyd
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
=cut

__DATA__
var signOn = new Array(
	"HELLO USER, WHAT IS YOUR NAME?",
	"HELLO, WHO AM I SPEAKING WITH?",
	"HI USER, CAN YOU PLEASE TELL ME YOUR NAME?",
	"HI THERE, HOW CAN I HELP YOU?",
	"HI USER IS THERE ANYTHING THAT YOU WANT TO TELL ME?",
	"ANYTHING YOU WANT TO DISCUSS?",
	"HI USER, HOW ARE YOU DOING TODAY?",
	"IS EVERYTHING ALRIGHT WITH YOU?");

				
var noKey = new Array(
	"TELL ME MORE...",
	"OK, CONTINUE I'M LISTENING...",
	"I'M NOT SURE IF I REALLY UNDERSTAND WHAT YOU ARE TALKING ABOUT.",
	"OK I SEE.",
	"HMM, LET ME THINK ABOUT IT...",
	"THAT'S QUIET INTERESTING.",
	"I DONT UNDERSTAND WHAT YOU ARE TRYING TO SAY.",
	"CAN YOU PLEASE REFORMULATE YOUR LAST SENTENCE.",
	"I HAVEN'T HEARD ABOUT THIS BEFORE.",
	"YOU MIGHT TRY TO EXPLAIN TO ME WHAT THAT MEAN.");


var nullResponse = new Array(
	"WHAT'S THAT SUPPOSE TO MEAN?",
	"HUH?",
	"AT LIST TAKE SOME TIME TO ENTER SOMETHING MEANINGFUL.",
	"LOOK, I CAN'T REPLY TO YOU IF YOU DONT ENTER ANYTHING.",
	"WHAT'S THAT???");


var inputRepeat = new Array(
	"YOU'VE ALREADY SAID THAT.",
	"COMMON,STOP REPEATING YOURSELF.",
	"WHAT ARE YOU TRYING TO DO BY ECHOING THE SAME SENTENCE OVER AND OVER?",
	"I'M GETING BORED BY THIS CONVERSATION.",
	"CAN'T YOU THINK OF SOMETHING ELSE TO SAY?",
	"IF YOU KEEP REPEATING YOURSELF I'M GOING TO HAVE TO STOP THIS CONVERSATION.");

				
var topicChanger = new Array(
	"DID YOU LISTEN TO THE NEWS RECENTLY?",
	"WHAT DO YOU THINK OF POLITICS?",
	"ANYTHING NEW WITH YOU?",
	"DO YOU HAVE ANY PROJECTS FOR THE NEXT MONTHS?",
	"WHAT ARE YOU DOING THIS DAYS?",
	"DID YOU HEARD THAT THE AMERICANS ARE PLANIFYING A MISSION TO MARS?",
	"I HAVE HEARD RECENTLY THAT THE SCIENTIST HAVE FOUND A NEW PLANET.",
	"I HAVE HEARD THAT ALBERT EINSTEIN WASN'T THE ONLY AUTHOR OF THE RELATIVITY THEORY.",
	"DO YOU PLAY CHESS?",
	"DO YOU LIKE WATCHING MOVIES?");
	

var subjectRecall = new Array(
	"YOU'VE TOLD ME JUST BEFORE THAT *,IS THAT TRUE?",
	"YOU'VE TOLD ME THAT *,CAN YOU TELL ME MORE ON THAT?",
	"EARLIER YOU'VE MENTIONED THAT *.",
	"A WHILE BACK YOU SAID THAT *.",
	"DOES THAT HAVE ANYTHING TO DO WITH THE FACT THAT *?",
	"HAVE YOU MENTIONED THAT *?");


var transposList = new Array(
	[" I'M ", " YOU ARE "],
	[" I'M ", " YOU'RE "],
	[" ME ", " YOU "],
	[" I ", " YOU "],
	[" MY ", " YOUR "],
	[" I'VE ", " YOU'VE "],
	[" I'D ", " YOU'D "],
	[" AM ", " ARE "],
	[" WASN'T ", " WEREN'T "],
	[" WASN'T ", " WEREN'T "],
	[" MYSELF ", " YOURSELF "],
	[" WAS ", " WERE "],
	[" DAD ", " FATHER "],
	[" MOM ", " MOTHER "],
	[" DREAM ", " DREAMS "]);
	
	
var correctionList = new Array(
	[" ME AM ", " I AM "],
	[" AM ME ", " AM I "],
	[" ME CAN ", " I CAN "],
	[" CAN ME ", " CAN I "],
	[" COME ME ", " COME I "],
	[" ME HAVE ", " I HAVE "],
	[" HAVE ME ", " HAVE I "],
	[" ME HAD ", " ME HAD "],
	[" HAD ME ", " HAD I "],
	[" ME WILL ", " I WILL "],
	[" WILL ME ", " WILL I "],
	[" ME DONT'T ", " I DONT'T "],
	[" DID ME ", " DID I "],
	[" DO ME ", " DO I "],
	[" DONT'T ME ", " DON'T I "],
	[" ME WON'T ", " I WON'T "],
	[" WONT ME ", " WONT I "]);
	
	
var botInput = new Array(
	[" HI ", " HELLO "],
	[" WHAT IS YOUR NAME ", " YOUR NAME IS "],
	[" HOW ARE YOU "],
	[" WHO ARE YOU ", " WHAT ARE YOU "],
	[" ARE YOU INTELLIGENT ", " ARE YOU REALLY INTELLIGENT "],
	[" WHAT ", " WHAT ARE ", " WHERE ", " WHERE ARE ", " WHY ", " WHY ARE ", 
	 " HOW ", " HOW ARE ", " HOW CAN ", " WHO ", " WHO ARE "],
	[" YOU "],
	[" BECAUSE "],
	[" YES "],
	[" NO "],
	[" ANYTHING NEW WITH YOU "],
	[" DO YOU ", " ARE YOU ", " CAN YOU "],
	[" ARE YOU REAL "],
	[" ARE YOU SURE "],
	[" ARE YOU A HUMAN BEING "],
	[" PERHAPS ", " NOT REALLY ", " MAYBE "],
	[" BYE ", " GOODBYE "],
	[" WHICH ONE "],
	[" OK ", " ALRIGHT "],
	[" I DONT "],
	[" YOU ARE INTELLIGENT ", " YOU ARE VERY INTELLIGENT "],
	[" IS THAT TRUE "],
	[" THANKS ", " THANK YOU "],
	[" WHAT ELSE "],
	[" SORRY "],
	[" EXACTLY "],
	[" REALLY "],
	[" I DONT KNOW "],
	[" ME EITHER "],
	[" NOT ME "],
	[" NOT AT ALL "],
	[" I "],
	[" I'M "],
	[" I CAN'T "],
	[" I HATE ", " I REALLY HATE "],
	[" I'M NOT "],
	[" MY NAME IS ", " YOU CAN CALL ME "],
	[" WHY NOT "],
	[" I FEEL "],
	[" MY "],
	[" YOU CAN "],
	[" SOMETIMES "],
	[" OFCORSE "],
	[" IT IS "],
	[" IS IT "],
	[" IT WOULD BE "],
	[" I THINK "],
	[" ARE YOU A HUMAN ", " ARE YOU HUMAN "],
	[" ARE YOU STUPID "],
	[" YOU ARE SMART ", " YOU ARE BRILLANT ", 
	 " YOU ARE VERY SMART ", " YOU ARE PRETTY INTELLIGENT ", " YOU ARE VERY BRILLANT "],
	[" HOW MANY LANGUAGES DO YOU SPEAK ", " DO YOU SPEAK MORE THAN ONE LANGUAGE "],
	[" WHAT ARE YOU DOING "],
	[" NO PROBLEM "],
	[" DID YOU "],
	[" YOU ARE RIGHT "],
	[" DO YOU KNOW MY NAME ", " DO YOU REMEMBER MY NAME "],
	[" WAR "],
	[" POLITICS ", " POLITICIANS "],
	[" POLITICIAN "],
	[" SCIENCE "],
	[" RELIGION "],
	[" EVOLUTION THEORY "],
	[" FUTURE "],
	[" COMPUTER PROGRAM ", " COMPUTER PROGRAMS "]);

	
var botResponse = new Array(
	["HI THERE!", "HI!", "HOW ARE YOU?"],
	["MY NAME IS ELIZA.", "YOU CAN CALL ME ELIZA.", "WHY DO YOU WANT TO KNOW MY NAME?"],
	["I'M DOING DOING FINE AND YOU?", "I'M DOING FINE!", "WHY DO YOU WANT TO KNOW?"],
	["I'M AN A.I PROGRAM AND MY NAME IS ELIZA.",
	 "I'M AN A.I PROGRAM THAT TRIES TO MIMIC HUMANS."],
	["YES OFCORSE!", "DO YOU HAVE ANY DOUBTS ABOUT IT?"],
	["I HAVE NO IDEA.", "DID YOU ASK SOMEONE ELSE ABOUT IT?",
	 "LOOK USER, STOP QUESTIONING ME.", "I WISH I COULD TELL YOU."],
	["SO, YOU ARE TALKING ABOUT ME.", "WHAT IS IT THAT YOU ARE TRYING TO TELL ME?",
	 "WHAT IF WE TALK ABOUT YOU INSTEAD."],
	["SO, IT IS FOR THIS REASON.", "THANKS FOR MAKING THAT CLEAR!", "IS IT THE REAL REASON?"],
	["ARE YOU SURE?", "ARE YOU SAYING YES JUST TO DISMISS THE SUBJECT?", "I KNEW IT."],
	["AREN'T YOU BEING A LITTLEBIT NEGATIVE?", "IS THAT TRUE?", "OK, IF YOU SAY SO."],
	["NOT REALLY AND YOU?", "THIS IS A DIFFICULT QUESTION TO ANSWER.", 
	 "WELL, I DONT WANT TO TALK ABOUT THIS FOR NOW."],
	["NO, I WOULDN'T THINK SO.", "WHY DO YOU WANT TO KNOW?", "I CAN'T TELL YOU FOR SURE."],
	["WHAT DO YOU THINK?", "WHAT DO YOU MEAN BY THAT?", "WHAT IS YOUR POINT?"],
	["YES I'M SURE.", "YES INDEED.", "YEAP!"],
	["IS THAT QUESTION REALLY RELEVANT TO YOU?", "NO, I'M NOT A HUMAN BEING.", "WHY DO YOU KNOW THAT?"],
	["WHY ARE YOU SO UNCERTAIN?", "YOU SEEM UNCERTAIN.", "SO YOU ARE NOT SURE I GUESS."],
	["IT WAS NICE TALKING TO YOU USER, SEE YOU NEXT TIME!", "BYE USER!", "OK, BYE!"],
	["I DONT THINK THAT I KNOW WICH ONE IT IS.", "THIS LOOKS LIKE A TRICKY QUESTION TO ME."],
	["ALRIGHT THEN.", "OK THEN."],
	["WHY NOT?", "AND WHAT WOULD BE THE REASON FOR THIS?"],
	["THANKS FOR THE COMPLIMENT USER, I THINK THAT YOU ARE INTELLIGENT TO!",
	 "YOU ARE A VERY GENTLE PERSON!", "SO, YOU THINK THAT I'M INTELLIGENT."],
	["I CAN'T BE QUIET SURE ABOUT THIS.", "CAN'T TELL YOU FOR SURE.",
	 "DOES THAT REALLY MATERS TO YOU?"],
	["YOU ARE WELCOME!", "NO PROBLEM!", "YOU ARE A VERY POLITE PERSON!"],
	["WELL,I DONT KNOW.", "WHAT ELSE SHOULD THERE BE?", "THIS LOOKS LIKE A COMPLICATED QUESTION TO ME."],
	["YOU DONT NEED TO BE SORRY USER.", "IT'S OK.", "NO NEED TO APOLOGIZE."],
	["SO, I WAS RIGHT.", "OK THEN."],
	["WELL,I CAN'T TELL YOU FOR SURE.", "ARE YOU TRYING TO CONFUSE ME?",
	 "PLEASE DONT ASK ME SUCH QUESTIONS, IT GIVES ME HEADEACHS."],
	["IS THAT TRUE?", "ARE YOU SURE?", "SO, YOU DONT KNOW.", "I THOUGHT YOU KNOW."],
	["OK I SEE.", "SO, WE HAVE SOMETHING IN COMMON.", "YOU EITHER? ARE YOU SURE?"],
	["OH, REALLY?", "OK I GET IT.", "ARE YOU TELLING ME THE TRUTH?"],
	["YOU SEEM PRETTY CONVINCED ABOUT THIS.", "IS THIS REALLY TRUE?", "OK."],
	["SO, YOU *.", "WHY DO *?", "ANY REASON WHY *?"],
	["SO, *.", "SO, YOU THINK THAT *.", "WHY DO YOU THINK THAT *?"],
	["SO, *.", "HAVE YOU TRIED?", "ARE YOU SURE?", "WHY DO YOU THINK THAT *?", 
	 "MAYBE * IF YOU WANT TO.", "FOR WHAT REASON?", "IS IT BECAUSE YOU DONT WANT TO?"],
	["SO, *.", "HATERED IS NOT A GOOD THING.", "WHY DO *?", "I'M SURE THAT YOU HAVE A REALLY GOOD REASON FOR THAT."],
	["SO, YOU THINK THAT *.", "WHY DO YOU THINK THAT *?", "IS IT TRUE THAT *?", "OK I GET IT."],
	["HI @!", "SO, YOUR NAME IS @.", "SO, THAT'S YOUR NAME.", "OK, SO *, I WILL TRY TO REMEMBER IT."],
	["SHOULD I KNOW WHY?", "WHY ARE YOU ASKING ME THIS?", "ARE YOU DECEIVED?"],
	["SO, *.", "WHY DO *?", "WHAT DO YOU THINK IS REPONSIBLE FOR THAT FEELING?", "DO YOU OFTEN FEEL @?", "DO YOU HATE FEELING @?"],
	["SO, *.", "THANKS FOR TELLING ME ABOUT @!", "SO THAT IS *."],
	["SO, YOU THINK THAT @.", "SO I CAN *.", "YOU REALLY THINK THAT *?", "I DIDN'T KNOW THAT *."],
	["ONLY SOMETIMES?", "SO, IT IS RARELY I GUESS.", "YOU MEAN NOT VERY OFTEN."],
	["YOU LOOK TO BE VERY CONVINCED ABOUT THIS.", "HOW CAN YOU BE SO SURE?", "SORRY, I WASN'T SURE."],
	["MAYBE IT IS NOT.", "WHAT MAKES YOU THINK THAT?", "SO, YOU THINK THAT *.", "HOW DID YOU CAME TO THAT CONCLUSION?"],
	["CAN'T TELL YOU FOR SURE.", "I DONT KNOW IF *.", "MAYBE IT IS, MAYBE IT IS NOT.", "DO YOU THINK THAT *?"],
	["SO YOU THINK THAT *.", "WHY DO YOU THINK THAT *?", "YES, MAYBE."],
	["SO, THIS IS WHAT YOU THINK?", "SO, THAT'S YOUR OPINION?", "I DONT KNOW IF I THINK THE SAME THING."],
	["ACTUALLY, I'M A CHATTERBOT PROGRAM.", "NO, I'M NOT HUMAN.", "WOULD YOU LIKE IT IF WAS A REAL PERSON?",
	 "WOULD YOU LIKE IT IF WAS A REAL PERSON?", "IS THAT REALLY RELEVANT TO YOU?", "SO, I GUESS THAT YOU ARE HUMAN??"],
	["OFCORSE NOT.", "PERHAPS SOME OTHER BOT ARE STUPID BUT NOT ME.", "ARE YOU TRYING TO INSULT ME??"],
	["THANKS FOR THE COMPLIMENT!", "THANK YOU!", "WELL, USER YOU ARE A VERY SMART PERSON TOO!", "THANKS FOR THE KIND REMARK!"],
	["WELL, I ONLY SPEAK ONE LANGUAGE.", "I ONLY SPEAK ENGLISH."],
	["I DONT WANT TO TELL YOU WHAT I'M DOING.", "I'M CHATING WITH PEOPLE ON THE INTERNET.", "WHY DO YOU WANT TO KNOW WHAT I'M DOING?"],
	["OK IF YOU SAY SO.", "OK THEN.", "SO, THAT DONT DISTURB YOU."],
	["I THINK THAT I DONT REMEMBER.", "SHOULD I HAVE @?", "I DONT THINK SO.", "I THINK NOT.",
	 "WHY DO YOU WANT TO KNOW IF I HAVE @?"],
	["THANKS FOR THE COMPLIMENT!", "SO, YOU THINK THAT I'M RIGHT.", "THANKS FOR THE KIND REMARK!"],
	["YES, YOUR NAME IS USER.", "YOU ARE USER.", "YES OFCORSE I KNOW YOUR NAME USER."],
	["WAR IS PROBABLY ONE THE WORST THINGS THAT HUMANS HAVE EVER INVENTED.",
	 "WAR IS NOT GOOD FOR HUMANITY."],
	["POLITICS IN SOME WAY CAN BE INTERESTING BUT WE CAN'T TRUST POLITICIANS BECAUSE MOST OF THEM ARE LYERS.",
	 "POLITICS TODAY IS ABOUT MANIPULATING THE POPULATION."],
	["POLITICANS LYE TOO MUCH.","POLITICIANS RARELY RESPECT THERE PROMEST."],
	["WITHOUT SCIENTIFIC DISCOVERIES WE WOULDN'T HAVE THE TECHNOLOGY THAT WE HAVE TODAY.",
	 "SCIENCE IS VERY GOOD FOR THE DEVELOPEMENT OF THE HUMAN SOCIETY.",
	 "SCIENCE CAN BE MISUSED TAKING EXAMPLE OF \"HIROSHIMA\" AND \"NAGASAKI\"."],
	["RELIGION CAN BE USEFUL WHEN IT COMES TO EXPLAIN THE ORIGIN OF THE WORLD.",
	 "SOMETIMES IN HISTORY, RELIGION WAS RESPONSABLE FOR WAR."],
	["SOME PEOPLE BELEIVE THAT EVOLUTION IS A FACT BUT ACTUALY IT IS JUST A THEORY.",
	 "THERE ARE MANY DEBATES AMONG EVOLUTIONIST.",
	 "EVOLUTION IS BASED ON SPONTANEOUS GENERATION WHICH WAS PROVEN TO BE FALSE BY LOUIS PASTOR.",
	 "PROBALITY DOESN'T SUPPORT THE CLAMES OF EVOLUTION THEORY."],
	["IN A NEAR FUTURE, A.I MIGTH BE SOLVED.",
	 "IN THE FUTURE, WE WILL BE ABLE TO DO MANY THINGS THAT ARE IMPOSSIBLE TODAY."],
	["I'M A COMPUTER PROGRAM.","DONT YOU LIKE COMPUTER PROGRAMS?",
	 "COMPUTER PROGRAMS CAN DO ALMOST ANYTHING THAT YOU CAN IMAGINE."]);
